﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAzure;
using System.IO;

namespace AzurePutBllockExample
{
    public partial class MainForm : Form
    {
        private AzureBlobUtil _blobUtil = new AzureBlobUtil();

        public MainForm()
        {
            InitializeComponent();
        }

        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            if (OpenDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileImage.ImageLocation = OpenDialog.FileName;
            }
        }

        private Uri _blobUri;

        private void UploadButton_Click(object sender, EventArgs e)
        {
            // Upload
            _blobUri = _blobUtil.UploadBlob(
                FileImage.ImageLocation, 
                CloudStorageAccount.DevelopmentStorageAccount, 
                "files");

            // Download as file
            Stream stream = _blobUtil.DownloadBlobAsStream(
                CloudStorageAccount.DevelopmentStorageAccount,
                _blobUri.ToString()
                );

            // Download as stream
            _blobUtil.DownloadBlobAsFile(
                CloudStorageAccount.DevelopmentStorageAccount,
                _blobUri.ToString()
                );

            // Display
            AzureImage.Image = Image.FromStream(stream);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_blobUri == null)
            {
                MessageBox.Show("Please upload a file!");
                return;
            }

            string fileName = _blobUtil.DownloadBlobAsFile(
                CloudStorageAccount.DevelopmentStorageAccount,
                _blobUri.ToString()
                );

            MessageBox.Show("Downloaded to: " + fileName);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (_blobUri == null)
            {
                MessageBox.Show("Please upload a file!");
                return;
            }

            Stream stream = _blobUtil.DownloadBlobAsStream(
                CloudStorageAccount.DevelopmentStorageAccount,
                _blobUri.ToString()
                );

            MessageBox.Show("Downloaded as Stream - Length: " + stream.Length.ToString());
        }
    }
}
